{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 1\n",
    "try:\n",
    "    def determine_weight(volume, density):\n",
    "        if density <= 0:\n",
    "            raise ValueError('Density must be positive')\n",
    "    \n",
    "    determine_weight(1, 0)\n",
    "except:\n",
    "    logging.exception('Expected')\n",
    "else:\n",
    "    assert False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 2\n",
    "# my_module.py\n",
    "class Error(Exception):\n",
    "    \"\"\"Base-class for all exceptions raised by this module.\"\"\"\n",
    "\n",
    "class InvalidDensityError(Error):\n",
    "    \"\"\"There was a problem with a provided density value.\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 3\n",
    "class my_module(object):\n",
    "    Error = Error\n",
    "    InvalidDensityError = InvalidDensityError\n",
    "\n",
    "    @staticmethod\n",
    "    def determine_weight(volume, density):\n",
    "        if density <= 0:\n",
    "            raise InvalidDensityError('Density must be positive')\n",
    "try:\n",
    "    weight = my_module.determine_weight(1, -1)\n",
    "    assert False\n",
    "except my_module.Error as e:\n",
    "    logging.error('Unexpected error: %s', e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 4\n",
    "weight = 5\n",
    "try:\n",
    "    weight = my_module.determine_weight(1, -1)\n",
    "    assert False\n",
    "except my_module.InvalidDensityError:\n",
    "    weight = 0\n",
    "except my_module.Error as e:\n",
    "    logging.error('Bug in the calling code: %s', e)\n",
    "\n",
    "assert weight == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 5\n",
    "weight = 5\n",
    "try:\n",
    "    weight = my_module.determine_weight(1, -1)\n",
    "    assert False\n",
    "except my_module.InvalidDensityError:\n",
    "    weight = 0\n",
    "except my_module.Error as e:\n",
    "    logging.error('Bug in the calling code: %s', e)\n",
    "except Exception as e:\n",
    "    logging.error('Bug in the API code: %s', e)\n",
    "    raise\n",
    "\n",
    "assert weight == 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 6\n",
    "# my_module.py\n",
    "class NegativeDensityError(InvalidDensityError):\n",
    "    \"\"\"A provided density value was negative.\"\"\"\n",
    "\n",
    "def determine_weight(volume, density):\n",
    "    if density < 0:\n",
    "        raise NegativeDensityError"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 7\n",
    "try:\n",
    "    my_module.NegativeDensityError = NegativeDensityError\n",
    "    my_module.determine_weight = determine_weight\n",
    "    try:\n",
    "        weight = my_module.determine_weight(1, -1)\n",
    "        assert False\n",
    "    except my_module.NegativeDensityError as e:\n",
    "        raise ValueError('Must supply non-negative density') from e\n",
    "    except my_module.InvalidDensityError:\n",
    "        weight = 0\n",
    "    except my_module.Error as e:\n",
    "        logging.error('Bug in the calling code: %s', e)\n",
    "    except Exception as e:\n",
    "        logging.error('Bug in the API code: %s', e)\n",
    "        raise\n",
    "except:\n",
    "    logging.exception('Expected')\n",
    "else:\n",
    "    assert False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 8\n",
    "# my_module.py\n",
    "class WeightError(Error):\n",
    "    \"\"\"Base-class for weight calculation errors.\"\"\"\n",
    "\n",
    "class VolumeError(Error):\n",
    "    \"\"\"Base-class for volume calculation errors.\"\"\"\n",
    "\n",
    "class DensityError(Error):\n",
    "    \"\"\"Base-class for density calculation errors.\"\"\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
